﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using NielsDerdaele.HttpSocket.Example.Server.DTO;

namespace NielsDerdaele.HttpSocket.Example.Server {
    public class ChatClient {
        private StreamReader sr;
        private StreamWriter sw;
        private Stream stream;
        private XmlSerializer serializer = new XmlSerializer(typeof(ChatMessage));

        public ChatClient(Stream stream) {
            this.stream = stream;
            sr = new StreamReader(stream, UnicodeEncoding.UTF8);
            new Thread(new ThreadStart(processData)).Start();
        }
        private void processData() {
            while (true) {
                //stream.
                byte[] buffer = new byte[2048];
                int read = stream.Read(buffer, 0, buffer.Length);
                
                using (MemoryStream m = new MemoryStream(buffer)) {
                    var obj = (ChatMessage)serializer.Deserialize(m);
                    System.Console.WriteLine(obj.Username + ": " + obj.Message);
                }                
            }
        }
    }
}
